<template>
	<view>
		<page-head :title="name" :bgColor="`transparent`" :headtype="1"></page-head>
		<image class="mainbg" src="@/static/img/bg.png" mode=""></image>
		<image class="logo" src="@/static/logo.png" mode="aspectFill"></image>
		<view class="btn mainBg" @click="getUserToken">授权登录</view>
	</view>
</template>

<script>
	import wxSDK from '@/utils/wxSDK.js';
	export default {
		data() {
			return {
				sqtype:'',  // 1提现
				logo:'',
				name:'',
				appid:this.apifun.appid,
				configInfo:{},
				inviteCode:'',
				inviteusermsg:{},
			};
		},
		onLoad(option) {
			// console.log(option);
			this.sqtype = option.sqtype || 1;
			this.inviteCode = option.invite_code || '';
			if (uni.getStorageSync('configInfo')) {
				this.configInfo = uni.getStorageSync('configInfo')
			};
			// this.getConfigInfo();
			// if (this.inviteCode) {
			// 	this.getUserInfoByCode();
			// };
			// 获取地址中的code
			let hrefurl = window.location.href;
			let code = this.getUrlParam(hrefurl,'code');
			if(!!code){
				this.getAccessToken(code);
			}
		},
		
		methods: {
			getUserToken(){  // 登录
				if (wxSDK && wxSDK.isWechat()) {
					let APPID = this.appid;
					let redirect_uri = encodeURIComponent(this.apifun.hrefURL+'partner/#/pages/start/fenglogo?invite_code='+this.inviteCode);
					let params = `appid=${APPID}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
					let wx =`https://open.weixin.qq.com/connect/oauth2/authorize?${params}`;
					window.location.href = wx;
				}
			},
			getAccessToken(code){  // 将code传给后台
				let sendData = {
					code: code,
					invite_code: this.inviteCode || ''
				}
				this.apifun.unirequest('/partner/user/wxScope','post',sendData,(res)=>{
					if(res.code === 200){
						let datas = res.data;
							let timestamp = Date.parse(new Date());
							window.location.href = this.apifun.hrefURL + "partner/#/pages/tabbar/my?ver="+timestamp;
					}else{
						this.apifun.toast(res.msg)
					}
				},true)
			},
			getUserInfoByCode(){  // 获取邀请人信息
				let  sendData = {
					invite_code: this.inviteCode
				};
				this.apifun.unirequest('/user/getUserInfoByCode','post',sendData,(res)=>{
					if(res.code === 200){
						let datas = res.data;
						this.inviteusermsg = datas;
					}else{
						this.apifun.toast(res.msg)
					}
				},true)
			},
			getConfigInfo(){  // 获取配置信息【作废】
				this.apifun.unirequest('/index/getConfigInfo','post',{},(res)=>{
					if(res.code === 200){
						let datas = res.data;
						uni.setStorageSync('configInfo',datas);
						this.logo = datas.logo;
						this.appid = datas.wechat_app_id;
						uni.setNavigationBarTitle({
							title: datas.site_title
						});
					}else{
						this.apifun.toast(res.msg)
					}
				},true)
			},
			getUrlParam(url,name) {
			    var pattern = new RegExp("[?&]"+name+"\=([^&]+)", "g");
			    var matcher = pattern.exec(url);
			    var items = null;
			    if(null != matcher){
			        try{
			            items = decodeURIComponent(decodeURIComponent(matcher[1]));
			        }catch(e){
			            try{
			                items = decodeURIComponent(matcher[1]);
			            }catch(e){
			                items = matcher[1];
			            }
			        }
			    }
			    return items;
			}	
		},
	};
</script>

<style>
	.mainbg{
		position: fixed;
		left: 0;
		top: 0;
		width: 100vw;
		height: 100vh;
	}
	.logo{
		position: fixed;
		left: 50%;
		top: 50%;
		width:222rpx;
		height: 252rpx;
		margin-left:-111rpx;
		margin-top: -252rpx;
	}
	.btn{
		position: fixed;
		left: 50%;
		bottom:300rpx;
		width:600rpx;
		margin-left: -300rpx;
		border-radius: 20rpx;
	}
</style>